<template>
  <div class="app-container">
    <el-page-header
      :content="getTitle"
      style="margin-bottom: 10px"
      @back="goBack"
    ></el-page-header>
    <div v-if="!isShow.none" v-loading="loading">
      <div
        v-if="loading"
        v-loading="loading"
        style="width: 100%; height: 850px"
      ></div>
      <el-card v-if="isShow['Basicinfo']">
        <BaseCharts
          :optimization-data="basicinfoData"
          x-title="No."
          y-title="Thick"
        ></BaseCharts>
      </el-card>
      <el-card v-if="isShow['Sort']">
        <BaseCharts
          :optimization-data="sortData"
          x-title=""
          y-title=""
        ></BaseCharts>
      </el-card>
      <el-card v-if="isShow.Result_lastOne">
        <BaseCharts
          :optimization-data="resultLastOneData"
          x-title=""
          y-title=""
        ></BaseCharts>
      </el-card>
      <el-card v-if="isShow.Result_lastTwo">
        <BaseCharts
          :optimization-data="resultLastTwoData"
          x-title=""
          y-title=""
        ></BaseCharts>
      </el-card>
      <el-card v-if="isShow.Result_lastThird">
        <BaseCharts
          :optimization-data="resultLastThirdData"
          x-title=""
          y-title=""
        ></BaseCharts>
      </el-card>
    </div>
    <div v-if="isShow.none" align="center">暂无数据</div>
  </div>
</template>

<script>
  import { getAction } from '@/api/action'
  import BaseCharts from '@/views/calculate-project/compoents/BaseCharts'
  export default {
    name: 'StructuralOptimizationCharts',
    components: { BaseCharts },
    data() {
      return {
        url: {
          getOptimizationData:
            '/presoftasim/calculateProject/structuralOptimizationCalculation/getOptimizationData',
          getOneFileData:
            '/presoftasim/calculateProject/structuralOptimizationCalculation/getOptimizationDataByFileOid',
        },
        basicinfoData: undefined, //basicinfo图表数据
        sortData: {}, //sort图表数据
        resultLastOneData: {}, //result图表数据
        resultLastTwoData: {}, //result图表数据
        resultLastThirdData: undefined, //result图表数据
        loading: true,
        isShow: {
          //要显示的图表
          Basicinfo: true,
          Sort: true,
          Result_lastOne: true,
          Result_lastTwo: true,
          Result_lastThird: true,
          nOne: false,
        },
        currentCal: {}, //当前计算项目
        file: {}, //当前要绘制图表的源文件
      }
    },
    computed: {
      getTitle() {
        if (this.file.originName == undefined) {
          return this.currentCal.name + '-Result'
        } else {
          return this.currentCal.name + '-' + this.file.originName
        }
      },
    },
    watch: {},
    created() {
      this.getCurrentCal()
    },
    methods: {
      //  获取Result优化趋势图数据
      getOptimizationData: function () {
        getAction(this.url.getOptimizationData, {
          oid: this.currentCal.oid,
          chartsType: 'Result',
        })
          .then((response) => {
            if (response.code === 200) {
              // this.submitOk(response.message);
              let notShow = [
                'Basicinfo',
                'Sort',
                'Result_lastOne',
                'Result_lastTwo',
                'Result_lastThird',
              ]
              for (let i in response.data) {
                if (response.data[i].type == 'Basicinfo') {
                  this.basicinfoData = response.data[i]
                  notShow.splice(notShow.indexOf('Basicinfo'), 1)
                } else if (response.data[i].type == 'Sort') {
                  this.sortData = response.data[i]
                  notShow.splice(notShow.indexOf('Sort'), 1)
                } else if (response.data[i].type == 'Result_lastOne') {
                  this.resultLastOneData = response.data[i]
                  notShow.splice(notShow.indexOf('Result_lastOne'), 1)
                } else if (response.data[i].type == 'Result_lastTwo') {
                  this.resultLastTwoData = response.data[i]
                  notShow.splice(notShow.indexOf('Result_lastTwo'), 1)
                } else if (response.data[i].type == 'Result_lastThird') {
                  this.resultLastThirdData = response.data[i]
                  notShow.splice(notShow.indexOf('Result_lastThird'), 1)
                }
              }
              if (notShow.length > 0) {
                this.hiddenCharts(notShow)
              }

              this.loading = false
            } else {
              this.submitFail(response.message)
            }
          })
          .catch((err) => {
            console.log(err)
          })
      },
      //  获取单个文件优化趋势图数据
      getOneFileData: function () {
        getAction(this.url.getOneFileData, {
          oid: this.currentCal.oid,
          fileList: [this.file.id],
        })
          .then((response) => {
            if (response.code === 200) {
              // this.submitOk(response.message);
              let notShow = [
                'Basicinfo',
                'Sort',
                'Result_lastOne',
                'Result_lastTwo',
                'Result_lastThird',
              ]
              for (let i in response.data) {
                if (response.data[i].type == 'Basicinfo') {
                  this.basicinfoData = response.data[i]
                  notShow.splice(notShow.indexOf('Basicinfo'), 1)
                } else if (response.data[i].type == 'Sort') {
                  this.sortData = response.data[i]
                  notShow.splice(notShow.indexOf('Sort'), 1)
                } else if (response.data[i].type == 'Result_lastOne') {
                  this.resultLastOneData = response.data[i]
                  notShow.splice(notShow.indexOf('Result_lastOne'), 1)
                } else if (response.data[i].type == 'Result_lastTwo') {
                  this.resultLastTwoData = response.data[i]
                  notShow.splice(notShow.indexOf('Result_lastTwo'), 1)
                } else if (response.data[i].type == 'Result_lastThird') {
                  this.resultLastThirdData = response.data[i]
                  notShow.splice(notShow.indexOf('Result_lastThird'), 1)
                }
              }
              if (notShow.length > 0) {
                this.hiddenCharts(notShow)
              }

              this.loading = false
            } else {
              this.submitFail(response.message)
            }
          })
          .catch((err) => {
            console.log(err)
          })
      },
      //隐藏无数据图表
      hiddenCharts: function (arr) {
        for (let i in arr) {
          this.isShow[arr[i]] = false
        }
        if (arr.length == 5) {
          this.isShow['nOne'] = true
        }
      },
      //  获取跳转地址参数
      getCurrentCal: function () {
        if (this.$route.query.target != null) {
          if (this.$route.query.target == 'Result') {
            //绘制Result图表
            this.currentCal = Object.assign({}, this.$route.query.currentCal)
            this.getOptimizationData()
          } else if (this.$route.query.target == 'one') {
            //绘制指定文件图表
            this.currentCal = Object.assign({}, this.$route.query.currentCal)
            this.file = Object.assign({}, this.$route.query.file)
            this.getOneFileData()
          }
        }
      },
      goBack() {
        this.$store.state.tagsView.visitedViews.splice(
          this.$store.state.tagsView.visitedViews.findIndex(
            (item) => item.path === this.$route.path
          ),
          1
        )
        this.$router.push({
          path: this.$store.state.tagsView.visitedViews[
            this.$store.state.tagsView.visitedViews.length - 1
          ].path,
          query: { target: this.currentCal },
        })
      },
    },
  }
</script>

<style scoped></style>
